﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;
using System.Reflection;
using System.Web.Compilation;
using System.Web.Script.Serialization;

public class HandlerHelper
{
    #region 转换数据
    /// <summary>
    /// 转换数据
    /// </summary>
    /// <param name="type"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    public static object ConvertValue(Type type, object value)
    {
        if (Convert.IsDBNull(value) || (value == null))
        {
            return null;
        }
        Type type2 = value.GetType();
        if (type == type2)
        {
            return value;
        }
        if (((type == typeof(Guid)) || (type == typeof(Guid?))) && (type2 == typeof(string)))
        {
            if (string.IsNullOrEmpty(value.ToString()))
            {
                return null;
            }
            return new Guid(value.ToString());
        }
        if (((type == typeof(DateTime)) || (type == typeof(DateTime?))) && (type2 == typeof(string)))
        {
            if (string.IsNullOrEmpty(value.ToString()))
            {
                return null;
            }
            return Convert.ToDateTime(value);
        }
        if (type.IsEnum)
        {
            try
            {
                return Enum.Parse(type, value.ToString(), true);
            }
            catch
            {
                return Enum.ToObject(type, value);
            }
        }
        if (((type == typeof(bool)) || (type == typeof(bool?))))
        {
            bool tempbool = false;
            if (bool.TryParse(value.ToString(), out tempbool))
            {
                return tempbool;
            }
            else
            {
                if (string.IsNullOrEmpty(value.ToString()))
                    return false;
                else
                    return true;
            }
        }
        if (type.IsGenericType)
        {
            type = type.GetGenericArguments()[0];
        }
        return Convert.ChangeType(value, type);
    }
    #endregion 

    public static object[] GetMethodParms(MethodInfo method, HttpContext context)
    {
        var parms = method.GetParameters();
        var objs = new object[parms.Length];
        for (var i = 0; i < parms.Length; i++)
        {
            var parm = parms[i];
            if (!string.IsNullOrEmpty(context.Request[parm.Name]))
            { 
                objs[i] = ConvertValue(parm.ParameterType, context.Request[parm.Name]);
            }
        }
        return objs;
    }
    public static MethodInfo GetMethod(string typeName,string methodName)
    {
        var Assemblies = System.Web.Compilation.BuildManager.GetReferencedAssemblies();
        foreach (Assembly assembly in Assemblies)
        { 
            var type = assembly.GetType(typeName);
            if (type == null) continue;
            return type.GetMethod(methodName);
        }
        return null;
    }

 
}